package com.hellogood.yaohuo.client.common;

import java.math.BigDecimal;

import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.EditCompletionEvent;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.events.EditorExitEvent;
import com.smartgwt.client.widgets.grid.events.EditorExitHandler;

public class OrgCodeEditorExitHandler implements EditorExitHandler {
	private ListGrid listGrid = null;
	private BasicVoSearchInterface basicVoSearchInterface;

	public OrgCodeEditorExitHandler(ListGrid listGrid, BasicVoSearchInterface basicVoSearchInterface) {
		this.basicVoSearchInterface = basicVoSearchInterface;
		this.listGrid = listGrid;
	}

	@Override
	public void onEditorExit(EditorExitEvent event) {
		int row = listGrid.getEditRow();
		int pluCol = listGrid.getFieldNum("pluCode");

		Object pluCode = listGrid.getEditedCell(row, pluCol);
		if (pluCode == null || pluCode.equals("")) {
			pluCode = listGrid.getEditValue(row, pluCol);
		}

		// boolean delete = false;
		Object amount = listGrid.getEditedCell(row, listGrid.getFieldName("amount"));
		if (row > 0
				&& (event.getEditCompletionEvent().getValue().equals(EditCompletionEvent.ESCAPE_KEYPRESS.getValue()) || event.getEditCompletionEvent().getValue()
						.equals(EditCompletionEvent.ENTER_KEYPRESS.getValue()))) {
			Object obj = listGrid.getEditedCell(row, 1);
			if (obj == null) {
				obj = listGrid.getEditValue(row, 1);
			}
			if ((obj == null || obj.toString().trim().equals("")) && (amount == null || amount.toString().equals("") || amount.toString().equals("0"))) {
				// delete = true;
				listGrid.cancelEditing();
				Record r = listGrid.getRecord(row);
				if (r != null) {
					listGrid.removeData(r);
				}
			}
		}
		int jPriceCol = listGrid.getFieldNum("jPrice");
		int psPriceCol = listGrid.getFieldNum("psPrice");
		if (event.getColNum() == jPriceCol) {
			int costCol = listGrid.getFieldNum("cost");
			Object priceObj = listGrid.getEditValue(row, jPriceCol);
			if (amount != null && priceObj != null) {
				BigDecimal bd = new BigDecimal(amount.toString());
				BigDecimal pr = new BigDecimal(priceObj.toString());
				pr = pr.multiply(bd);
				listGrid.setEditValue(row, costCol, pr.toString());
			}
		} else if (event.getColNum() == psPriceCol) {
			int psTotalCol = listGrid.getFieldNum("psCost");
			Object psPriceObj = listGrid.getEditValue(row, psPriceCol);
			if (amount != null && psPriceObj != null) {
				BigDecimal bd = new BigDecimal(amount.toString());
				BigDecimal pr = new BigDecimal(psPriceObj.toString());
				pr = pr.multiply(bd);
				listGrid.setEditValue(row, psTotalCol, pr.toString());
			}
		}

		if (event.getEditCompletionEvent().getValue().equals(EditCompletionEvent.DOWN_ARROW_KEYPRESS.getValue())) {

			if (pluCode != null && !pluCode.toString().equals("") && amount != null && Integer.valueOf(amount.toString()) > 0) {
				if (listGrid.saveAllEdits()) {
					listGrid.addData(new Record());
					listGrid.startEditing(row + 1, 1, false);
				}
			} else {
				event.cancel();
			}
		}

		if (pluCol == 1 && (pluCode == null || pluCode.toString().equals(""))) {
			if (event.getEditCompletionEvent().getValue().equals(EditCompletionEvent.ESCAPE_KEYPRESS.getValue())) {

			} else {
				event.cancel();
			}
		} else if (pluCol == 1 && (pluCode != null && !pluCode.toString().equals(""))) {
			basicVoSearchInterface.fillRowWithBasicVo();
		}
	}
}